using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Deepcode.Flux.Core.Services.Assistant
{
	/// <summary>
	/// ListUtil is a static utility class that provides
	/// various utility functions for operating on lists
	/// </summary>
	public static class ListUtil
	{
		/// <summary>
		/// Converts the CSV list passed in into an array of ints
		/// </summary>
		/// <param name="CSVOfInts"></param>
		/// <returns></returns>
		public static int[] GetCSVInts(string CSVOfInts)
		{
			string [] items = GetCSVStrings( CSVOfInts );
			List<int> ilist = new List<int>();

			foreach (string s in items)
			{
				try
				{
					int v  = Int32.Parse(s);
					ilist.Add( v );
				}
				catch { }
			}

			return ilist.ToArray();
		}
		/// <summary>
		/// Converts the CSV list passed in into an array of string
		/// </summary>
		/// <param name="CSVOfInts"></param>
		/// <returns></returns>
		public static string[] GetCSVStrings(string CSVOfInts)
		{
			string [] items = CSVOfInts.Split(',');
			return items;
		}
		/// <summary>
		/// De-dupes the ArrayList passed in to remove duplicated items
		/// </summary>
		/// <param name="list"></param>
		/// <returns></returns>
		public static ArrayList DeDupeList(object [] list)
		{
			ArrayList result = new ArrayList();
			foreach( object o in list )
				if( ! (result.Contains( o )) ) result.Add( o );

			return result;
		}
		/// <summary>
		/// Returns a string array as a CSV list using the seperator specified
		/// </summary>
		/// <param name="list"></param>
		/// <param name="seperator"></param>
		/// <returns></returns>
		public static string GetArrayAsCSV(object [] list, string seperator)
		{
			string result = "";
			foreach (object s in list)
			{
				if( result != "" ) result += seperator;
				result += s.ToString();
			}
			return result;
		}
	}
}
